{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import QUANTAXIS as QA\n",
    "import threading\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "user = QA.QA_User(username='admin',password='940809x')\n",
    "portfolio = user.new_portfolio('example')\n",
    "# 创建两个account\n",
    "#这里是创建一个资产组合,然后在组合里面创建两个account  你可以想象成股票里面的两个策略账户\n",
    "#然后返回的是这个账户的id\n",
    "a_1 = portfolio.new_account(account_cookie='a1')\n",
    "a_2 = portfolio.new_account(account_cookie='a2')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "< QA_Account a1 market: stock_cn>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a_1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'worker': < QA_Worker market_event id = 1766490941648 >, 'event': < QA_Event query_order None False , id = 1766490942264 >, 'res': None, 'callback': False, 'task_id': 'Task_evbcmD20', 'engine': 'ORDER'}\n",
      "receive tasks\n",
      "<QA_Market with ['backtest'] QA_Broker >\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "然后这里 是创建一个交易前置  你可以理解成 创建了一个无界面的通达信客户端\n",
    "然后start()开启这个客户端\n",
    "连接到backtest的broker上 这个broker可以更换\n",
    "\"\"\"\n",
    "# 创建一个交易前置\n",
    "market = QA.QA_Market()\n",
    "# 交易前置连接broker \n",
    "market.start()\n",
    "market.connect(QA.RUNNING_ENVIRONMENT.BACKETEST)\n",
    "\n",
    "# 打印market\n",
    "print(market)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "subscribe\n",
      "subscribe\n",
      "['a1', 'a2']\n"
     ]
    }
   ],
   "source": [
    "\n",
    "\"\"\"\n",
    "登陆到这个交易前置上 把你刚才的两个账户\n",
    "\"\"\"\n",
    "# 登陆交易\n",
    "market.login(QA.BROKER_TYPE.BACKETEST,a_1.account_cookie, a_1)\n",
    "market.login(QA.BROKER_TYPE.BACKETEST,a_2.account_cookie, a_2)\n",
    "# 打印市场中的交易账户\n",
    "print(market.get_account_cookie())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "#然后这里 往交易前置里面添加订单 这个操作是异步的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None by_money 2017-12-01 600010 2.62 CLOSE 1 100000\n",
      "{'worker': < QA_Worker market_event id = 1766490941648 >, 'event': < QA_Event receive_order None <bound method QA_Market.on_insert_order of <QA_Market with ['backtest'] QA_Broker >> , id = 1766491915096 >, 'res': None, 'callback': False, 'task_id': 'Task_aCeTp9RS', 'engine': 'ORDER'}\n",
      "receive tasks\n"
     ]
    }
   ],
   "source": [
    "market.insert_order(account_cookie=a_1.account_cookie, money=100000, amount=None,price=None, amount_model=QA.AMOUNT_MODEL.BY_MONEY,time='2017-12-01', code='600010', \n",
    "                    order_model=QA.ORDER_MODEL.CLOSE, towards=QA.ORDER_DIRECTION.BUY,market_type=QA.MARKET_TYPE.STOCK_CN,\n",
    "                   frequence=QA.FREQUENCE.DAY,broker_name=QA.BROKER_TYPE.BACKETEST)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "100 on_insert_orderby_amount\n",
      " < QA_Order realorder_id Order_ecHqxVwg datetime:2017-12-01 15:00:00 code:600010 amount:38100 price:2.62 towards:1 btype:stock_cn order_id:Order_ecHqxVwg account:a1 status:queued >2017-12-01\n",
      " queued000001\n",
      " 13.0 CLOSE 1 None\n",
      "{'worker': < QA_Worker market_event id = 1766490941648 >, 'event': < QA_Event receive_order None <bound method QA_Market.on_insert_order of <QA_Market with ['backtest'] QA_Broker >> , id = 1766491928616 >, 'res': None, 'callback': False, 'task_id': 'Task_RIXda7tM', 'engine': 'ORDER'}\n",
      "receive tasks\n"
     ]
    }
   ],
   "source": [
    "market.insert_order(account_cookie=a_1.account_cookie, amount=100,price=None, amount_model=QA.AMOUNT_MODEL.BY_AMOUNT,time='2017-12-01', code='000001', \n",
    "                    order_model=QA.ORDER_MODEL.CLOSE, towards=QA.ORDER_DIRECTION.BUY,market_type=QA.MARKET_TYPE.STOCK_CN,\n",
    "                   frequence=QA.FREQUENCE.DAY,broker_name=QA.BROKER_TYPE.BACKETEST)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\n下单以后 现金不会减少 但是可用现金会被扣除\\n因为如果是市价单 你的成交价未定\\n没法直接减少现金\\n可用现金减少 cash不减少 等到settle 等到成功交易的时候 才会扣cash\\n\\n'"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#\n",
    "\"\"\"\n",
    "下单以后 现金不会减少 但是可用现金会被扣除\n",
    "因为如果是市价单 你的成交价未定\n",
    "没法直接减少现金\n",
    "可用现金减少 cash不减少 等到settle 等到成功交易的时候 才会扣cash\n",
    "\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "898699.675"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "market.session[a_1.account_cookie].cash_available"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1000000]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "market.session[a_1.account_cookie].cash"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\n这里是交易前置内部的订单队列\\n'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "on_insert_order\n",
      "< QA_Order realorder_id Order_RQyS5C2v datetime:2017-12-01 15:00:00 code:000001 amount:100 price:13.0 towards:1 btype:stock_cn order_id:Order_RQyS5C2v account:a1 status:queued >\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "这里是交易前置内部的订单队列\n",
    "\"\"\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "queued\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'Order_ecHqxVwg': < QA_Order realorder_id Order_ecHqxVwg datetime:2017-12-01 15:00:00 code:600010 amount:38100 price:2.62 towards:1 btype:stock_cn order_id:Order_ecHqxVwg account:a1 status:queued >,\n",
       " 'Order_RQyS5C2v': < QA_Order realorder_id Order_RQyS5C2v datetime:2017-12-01 15:00:00 code:000001 amount:100 price:13.0 towards:1 btype:stock_cn order_id:Order_RQyS5C2v account:a1 status:queued >}"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "market.order_handler.order_queue()\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Order_ecHqxVwg': < QA_Order realorder_id Order_ecHqxVwg datetime:2017-12-01 15:00:00 code:600010 amount:38100 price:2.62 towards:1 btype:stock_cn order_id:Order_ecHqxVwg account:a1 status:queued >,\n",
       " 'Order_RQyS5C2v': < QA_Order realorder_id Order_RQyS5C2v datetime:2017-12-01 15:00:00 code:000001 amount:100 price:13.0 towards:1 btype:stock_cn order_id:Order_RQyS5C2v account:a1 status:queued >}"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "market.order_handler.order_queue.order_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "#pending 是指的待成交列表\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "market.order_handler.order_queue.pending"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\n这个_trade是一个私有方法 只有模拟盘和回测才会有 实盘就是真的交易了 \\n这个_trade是backtest类去调用的\\n\\n'"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"\"\"\n",
    "这个_trade是一个私有方法 只有模拟盘和回测才会有 实盘就是真的交易了 \n",
    "这个_trade是backtest类去调用的\n",
    "\n",
    "\"\"\"\n",
    "#market._trade(QA.QA_Event(broker_name=QA.BROKER_TYPE.BACKETEST,after_success=None))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'下面这两个是 查询  一个是异步查询 一个是同步的(no_wait)\\n异步不会阻塞当前线程 同步会阻塞'"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"\"\"下面这两个是 查询  一个是异步查询 一个是同步的(no_wait)\n",
    "异步不会阻塞当前线程 同步会阻塞\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'worker': < QA_Broker backtest thread 12956 >, 'event': < QA_Event query_data None <bound method QA_Market.on_query_data of <QA_Market with ['backtest'] QA_Broker >> , id = 1766491981136 >, 'res': None, 'callback': False, 'task_id': 'Task_5BxLAEUK', 'engine': 'backtest'}\n",
      "receive tasks\n",
      "ON QUERY\n",
      "[{'code': '600010', 'open': 2.6, 'high': 2.63, 'low': 2.56, 'close': 2.62, 'volume': 920022.0, 'amount': 239518192.0, 'date': '2017-12-01 00:00:00'}]"
     ]
    }
   ],
   "source": [
    "market.query_data(broker_name=QA.BROKER_TYPE.BACKETEST,frequence=QA.FREQUENCE.DAY,market_type=QA.MARKET_TYPE.STOCK_CN,\n",
    "                 code='600010',start='2017-12-01')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[{'code': '000001',\n",
       "  'open': 13.15,\n",
       "  'high': 13.31,\n",
       "  'low': 12.91,\n",
       "  'close': 13.0,\n",
       "  'volume': 1001998.0,\n",
       "  'amount': 1311131904.0,\n",
       "  'date': '2017-12-14 00:00:00'}]"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "market.query_data_no_wait(broker_name=QA.BROKER_TYPE.BACKETEST,frequence=QA.FREQUENCE.DAY,market_type=QA.MARKET_TYPE.STOCK_CN,\n",
    "                 code='000001',start='2017-12-14')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "< QA_Account a1 market: stock_cn>"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"\"\"成交了以后 你可以看到账户的资产变化了\"\"\"\n",
    "market.session[a_1.account_cookie]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'待成交列表被清空'"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"\"\"待成交列表被清空\"\"\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "market.order_handler.order_queue.pending"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'待成交队列清空'"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"\"\"待成交队列清空\"\"\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Order_ecHqxVwg': < QA_Order realorder_id Order_ecHqxVwg datetime:2017-12-01 15:00:00 code:600010 amount:38100 price:2.62 towards:1 btype:stock_cn order_id:Order_ecHqxVwg account:a1 status:success_all >,\n",
       " 'Order_RQyS5C2v': < QA_Order realorder_id Order_RQyS5C2v datetime:2017-12-01 15:00:00 code:000001 amount:100 price:13.0 towards:1 btype:stock_cn order_id:Order_RQyS5C2v account:a1 status:success_all >}"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "market.order_handler.order_queue.order_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Order_ecHqxVwg': < QA_Order realorder_id Order_ecHqxVwg datetime:2017-12-01 15:00:00 code:600010 amount:38100 price:2.62 towards:1 btype:stock_cn order_id:Order_ecHqxVwg account:a1 status:success_all >,\n",
       " 'Order_RQyS5C2v': < QA_Order realorder_id Order_RQyS5C2v datetime:2017-12-01 15:00:00 code:000001 amount:100 price:13.0 towards:1 btype:stock_cn order_id:Order_RQyS5C2v account:a1 status:success_all >}"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "market.order_handler.order_queue()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1000000, 900153.0445, 898848.0445]"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"\"\"\n",
    "cash 现金减少\n",
    "\"\"\"\n",
    "market.session[a_1.account_cookie].cash"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Series([], Name: amount, dtype: float64)"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"\"\"\n",
    "因为没有触发每日结算时间 在T+1的市场 即使买入了也没有可卖的\n",
    "\"\"\"\n",
    "market.session[a_1.account_cookie].sell_available"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "sa=market.session[a_1.account_cookie].sell_available"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "ac=market.session[a_1.account_cookie]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "code\n",
       "000001      100\n",
       "600010    38100\n",
       "Name: amount, dtype: int64"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ac.hold"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Series([], Name: amount, dtype: float64)"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sa"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[['2017-12-01 00:00:00',\n",
       "  '600010',\n",
       "  2.62,\n",
       "  38100,\n",
       "  900153.0445,\n",
       "  'Order_ecHqxVwg',\n",
       "  'Order_ecHqxVwg',\n",
       "  'Trade_XUE7pjI2',\n",
       "  'a1',\n",
       "  24.9555,\n",
       "  0,\n",
       "  None,\n",
       "  0],\n",
       " ['2017-12-01 00:00:00',\n",
       "  '000001',\n",
       "  13.0,\n",
       "  100,\n",
       "  898848.0445,\n",
       "  'Order_RQyS5C2v',\n",
       "  'Order_RQyS5C2v',\n",
       "  'Trade_4uoOCHic',\n",
       "  'a1',\n",
       "  5,\n",
       "  0,\n",
       "  None,\n",
       "  0]]"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "market.session[a_1.account_cookie].history"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "code\n",
       "000001      100\n",
       "600010    38100\n",
       "Name: amount, dtype: int64"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"\"\"\n",
    "持仓表增加\n",
    "\"\"\"\n",
    "market.session[a_1.account_cookie].hold"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'source': 'account',\n",
       " 'account_cookie': 'a1',\n",
       " 'portfolio_cookie': 'example',\n",
       " 'user_cookie': 'USER_wp6OZvjM',\n",
       " 'broker': 'backtest',\n",
       " 'market_type': 'stock_cn',\n",
       " 'strategy_name': None,\n",
       " 'current_time': 'None',\n",
       " 'allow_sellopen': False,\n",
       " 'allow_t0': False,\n",
       " 'margin_level': {},\n",
       " 'init_assets': {'cash': 1000000, 'hold': {}},\n",
       " 'init_cash': 1000000,\n",
       " 'init_hold': {},\n",
       " 'commission_coeff': 0.00025,\n",
       " 'tax_coeff': 0.001,\n",
       " 'cash': [1000000, 900153.0445, 898848.0445],\n",
       " 'history': [['2017-12-01 00:00:00',\n",
       "   '600010',\n",
       "   2.62,\n",
       "   38100,\n",
       "   900153.0445,\n",
       "   'Order_ecHqxVwg',\n",
       "   'Order_ecHqxVwg',\n",
       "   'Trade_XUE7pjI2',\n",
       "   'a1',\n",
       "   24.9555,\n",
       "   0,\n",
       "   None,\n",
       "   0],\n",
       "  ['2017-12-01 00:00:00',\n",
       "   '000001',\n",
       "   13.0,\n",
       "   100,\n",
       "   898848.0445,\n",
       "   'Order_RQyS5C2v',\n",
       "   'Order_RQyS5C2v',\n",
       "   'Trade_4uoOCHic',\n",
       "   'a1',\n",
       "   5,\n",
       "   0,\n",
       "   None,\n",
       "   0]],\n",
       " 'trade_index': ['2017-12-01 00:00:00', '2017-12-01 00:00:00'],\n",
       " 'running_time': '2019-02-05 18:41:05.250531',\n",
       " 'quantaxis_version': '1.3.0.dev3',\n",
       " 'running_environment': 'backtest',\n",
       " 'start_date': '2017-12-01',\n",
       " 'end_date': '2017-12-01'}"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"\"\"\n",
    "账户信息\n",
    "\n",
    "可以看到 减少的资产 主要是因为收了手续费\n",
    "\"\"\"\n",
    "market.session[a_1.account_cookie].message"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'worker': < QA_Account a1 market: stock_cn>, 'event': < QA_Event account_settle None False , id = 1766491915096 >, 'res': None, 'callback': False, 'task_id': 'Task_s8oSeczT', 'engine': 'backtest'}\n",
      "receive tasks\n",
      "{'worker': < QA_Account a2 market: stock_cn>, 'event': < QA_Event account_settle None False , id = 1766492196936 >, 'res': None, 'callback': False, 'task_id': 'Task_sR2I3ZMX', 'engine': 'backtest'}\n",
      "receive tasks\n",
      "{'worker': < QA_Broker backtest thread 12956 >, 'event': < QA_Event broker_settle None False , id = 1766492199736 >, 'res': None, 'callback': False, 'task_id': 'Task_DX3xul7R', 'engine': 'backtest'}\n",
      "receive tasks\n",
      "{'worker': < QA_Worker market_event id = 1766490941648 >, 'event': < QA_Event broker_settle None False , id = 1766492200520 >, 'res': None, 'callback': False, 'task_id': 'Task_FxgfQ4GK', 'engine': 'ORDER'}\n",
      "receive tasks\n",
      "===== SETTLED None =====\n",
      "account_settleSETTLE ORDERHANDLER\n",
      "\n"
     ]
    }
   ],
   "source": [
    "\"\"\"结算事件\"\"\"\n",
    "market._settle(QA.BROKER_TYPE.BACKETEST)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\n结算完以后 可卖数量就会变成和持仓数一样\\n'"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "orderahndle: trade\n",
      "{< QA_Account a1 market: stock_cn>: < QA_Broker backtest thread 12892 >, < QA_Account a2 market: stock_cn>: < QA_Broker backtest thread 12892 >}\n",
      "account_settle"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "结算完以后 可卖数量就会变成和持仓数一样\n",
    "\"\"\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "code\n",
       "000001      100\n",
       "600010    38100\n",
       "Name: amount, dtype: int64"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "broker settle"
     ]
    }
   ],
   "source": [
    "market.session[a_1.account_cookie].hold"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "code\n",
       "000001      100\n",
       "600010    38100\n",
       "Name: amount, dtype: int64"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "market.session[a_1.account_cookie].sell_available"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\n结算完以后 待成交队列也被清空\\n'"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"\"\"\n",
    "结算完以后 待成交队列也被清空\n",
    "\"\"\"\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{}"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "market.order_handler.order_queue()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "38100 by_amount 2017-12-05 600010 2.55 CLOSE -1 None\n",
      "{'worker': < QA_Worker market_event id = 1766490941648 >, 'event': < QA_Event receive_order None <bound method QA_Market.on_insert_order of <QA_Market with ['backtest'] QA_Broker >> , id = 1766492298488 >, 'res': None, 'callback': False, 'task_id': 'Task_D38SOwRm', 'engine': 'ORDER'}\n",
      "receive tasks\n"
     ]
    }
   ],
   "source": [
    "market.insert_order(account_cookie=a_1.account_cookie, amount=market.session[a_1.account_cookie].sell_available.get('600010',0),price=None, amount_model=QA.AMOUNT_MODEL.BY_AMOUNT,time='2017-12-05', code='600010', \n",
    "                    order_model=QA.ORDER_MODEL.CLOSE, towards=QA.ORDER_DIRECTION.SELL,market_type=QA.MARKET_TYPE.STOCK_CN,\n",
    "                   frequence=QA.FREQUENCE.DAY,broker_name=QA.BROKER_TYPE.BACKETEST)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{}"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "market.order_handler.order_queue.order_list\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{}"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "market.order_handler.order_queue()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "code\n",
       "000001    100\n",
       "600010      0\n",
       "Name: amount, dtype: int64"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "market.session[a_1.account_cookie].sell_available"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [],
   "source": [
    "#market._trade(QA.QA_Event(broker_name=QA.BROKER_TYPE.BACKETEST,after_success=None))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1000000, 900153.0445, 898848.0445]"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "market.session[a_1.account_cookie].cash"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[['2017-12-01 00:00:00',\n",
       "  '600010',\n",
       "  2.62,\n",
       "  38100,\n",
       "  900153.0445,\n",
       "  'Order_ecHqxVwg',\n",
       "  'Order_ecHqxVwg',\n",
       "  'Trade_XUE7pjI2',\n",
       "  'a1',\n",
       "  24.9555,\n",
       "  0,\n",
       "  None,\n",
       "  0],\n",
       " ['2017-12-01 00:00:00',\n",
       "  '000001',\n",
       "  13.0,\n",
       "  100,\n",
       "  898848.0445,\n",
       "  'Order_RQyS5C2v',\n",
       "  'Order_RQyS5C2v',\n",
       "  'Trade_4uoOCHic',\n",
       "  'a1',\n",
       "  5,\n",
       "  0,\n",
       "  None,\n",
       "  0]]"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "market.session[a_1.account_cookie].history"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "account a1 save\n",
      "account a2 save\n",
      "QAACCOUNT: THIS ACCOUNT DOESNOT HAVE ANY TRADE\n",
      "QAACCOUNT: THIS ACCOUNT DOESNOT HAVE ANY TRADE\n"
     ]
    }
   ],
   "source": [
    "user.save()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  },
  "latex_envs": {
   "LaTeX_envs_menu_present": true,
   "autoclose": false,
   "autocomplete": true,
   "bibliofile": "biblio.bib",
   "cite_by": "apalike",
   "current_citInitial": 1,
   "eqLabelWithNumbers": true,
   "eqNumInitial": 1,
   "hotkeys": {
    "equation": "Ctrl-E",
    "itemize": "Ctrl-I"
   },
   "labels_anchors": false,
   "latex_user_defs": false,
   "report_style_numbering": false,
   "user_envs_cfg": false
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
